<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <van-loading
      size="24px"
      vertical
      color="red"
      v-if="articleDetail.title === undefined"
      >加载中...</van-loading
    >
    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ articleDetail.title }}</h1>

        <!-- 用户信息 -->
        <van-cell
          center
          :title="articleDetail.aut_name"
          :label="totime(articleDetail.pubdate)"
        >
          <template #icon>
            <img :src="articleDetail.aut_photo" class="avatar" />
          </template>
          <template #default>
            <div>
              <van-button
                type="info"
                size="mini"
                v-if="articleDetail.is_followed"
                @click="followedfn(false)"
                >已关注</van-button
              >
              <van-button
                icon="plus"
                v-else
                type="info"
                size="mini"
                plain
                @click="followedfn(true)"
                >关注</van-button
              >
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="articleDetail.content"></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button
            icon="good-job"
            type="danger"
            size="small"
            v-if="articleDetail.attitude === 1"
            @click="likefncount(false)"
            >不喜欢</van-button
          >
          <van-button
            icon="good-job-o"
            v-else
            type="danger"
            plain
            size="small"
            @click="likefncount(true)"
            >喜欢</van-button
          >
        </div>
      </div>

      <!-- 文章评论区域 -->
      <div>
        <CommentList
          :artid="articleDetail.art_id"
          v-if="articleDetail.art_id"
        ></CommentList>
      </div>
    </div>
  </div>
</template>

<script>
import { totime } from '@/utils/date.js'
import CommentList from './CommentList'
import {
  articleDetailAPI,
  followedUserAPI,
  unFollowedUserAPI,
  likearticleAPI,
  unlikearticleAPI
} from '@/api'
export default {
  name: 'ArticleDetail',
  data () {
    return {
      articleDetail: {}
    }
  },
  components: {
    CommentList
  },
  async created () {
    const res = await articleDetailAPI({
      id: this.$route.query.aid
    })
    this.articleDetail = res.data.data
    this.articleDetail = res.data.data
    console.log(this.articleDetail)
  },
  methods: {
    totime,
    async followedfn (bool) {
      if (bool === true) {
        // 点击已关注页面是未关注
        this.articleDetail.is_followed = true
        await followedUserAPI({
          target: this.articleDetail.aut_id
        })
      } else {
        // 点击未关注页面是关注
        this.articleDetail.is_followed = false
        await unFollowedUserAPI({
          uid: this.articleDetail.aut_id
        })
      }
    },
    async likefncount (bool) {
      if (bool === true) {
        // 点击喜欢页面是不喜欢
        await likearticleAPI({
          target: this.articleDetail.art_id
        })
        this.articleDetail.attitude = 1
      } else {
        // 点击不喜欢页面是喜欢
        this.articleDetail.attitude = -1
        await unlikearticleAPI({
          artId: this.articleDetail.art_id
        })
      }
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
  padding-bottom: 1.22667rem;
}
.van-loading {
  padding-top: 1.52667rem;
}
</style>
